package com.bluebench.bigul.service.resolver;

import java.util.List;

import com.bluebench.bigul.domain.Element;
import com.bluebench.bigul.domain.Element.Attribute;
import com.bluebench.bigul.domain.Element.Parameter;
import com.bluebench.bigul.domain.FormData;
import com.bluebench.bigul.domain.Invocation;
import com.bluebench.bigul.exception.GraphicInitializationException;


public class ElementResolver extends DefaultExpressionResolver{
	
	
	public ElementResolver() throws GraphicInitializationException {
		if(!IS_INITIALIZED){
			IS_INITIALIZED = true;
			super.resolve();			
		}			
	}
	
	@Override
	public void resolve() throws GraphicInitializationException {
		
			String expression = tag.getExpression();
			Element element = (Element)tag;
									
			for(Element e : element.getElements()){
				e.setResolver(getInstance(ELEMENT_RESOLVER));
				e.resolve();
			}
			
			for(Attribute a : element.getAttributes()){
				a.setResolver(getInstance(ATTRIBUTE_RESOLVER));
				a.resolve();
			}
			
			Invocation invocation = element.getInvocation();
			if(invocation!=null){
				List<Parameter> parameters = invocation.getParameters();
				for(Parameter p : parameters){
					p.setResolver(getInstance(PARAMETER_RESOLVER));
					p.resolve();
				}
			}
			
			if(expression!=null && validate(expression)){
				resolveExpression(expression);
			}
		
	}	
	
	public static void main(String[] args) throws GraphicInitializationException {
		
		 
		/*GenericExpressionResolver.getInstance(GenericExpressionResolver.ELEMENT_RESOLVER)
		 .build("<default id=\"default\"><font-family id=\"font-family\"><attribute id=\"helvetica\" name=\"HELVETICA\" type=\"family\"/><attribute id=\"courier\" name=\"COURIER\" type=\"family\"/><attribute id=\"timesroman\" name=\"TIMES_ROMAN\" type=\"family\"/></font-family></default>");
		*/ 
		
		 ElementResolver eResolver = (ElementResolver)DefaultExpressionResolver.getInstance(DefaultExpressionResolver.ELEMENT_RESOLVER)
		 .build("<template id=\"credential\" name=\"CREDENTIAL_TEMPLATE\" type=\"template\"><global><!--Fonts,BaseColors,Position--><font-title id=\"title_font\" type=\"font\"><attribute id=\"color\"><param id=\"red\" type=\"_float\" value=\":red\"/><param id=\"blue\" type=\"_float\" value=\":blue\"/><param id=\"green\" type=\"_float\" value=\":green\"/></attribute><attribute id=\"family\" param=\"#default.font-family.helvetica\"/><attribute id=\"size\" type=\"float\" param=\"12.2\"/><attribute id=\"style\" param=\"bold\"/></font-title></global><page id=\"cover\" type=\"page\" order=\"0\"><logo id=\"logo\" type=\"image\"><param id=\"url\" name=\"url\" value=\":url\"/></logo><title id=\"title\" type=\"paragraph\"><attribute id=\"font\" param=\"#global.title_font\"/><attribute id=\"alignment\" param=\"#default.alignment.left\"/></title><subtitle id=\"subtitle\" ref=\"#credential.cover.title\"/><author id=\"author\" type=\"chunk\"><attribute id=\"background\" param=\"#default.base-color.blue\"/></author></page><page id=\"cover2\" ref=\"#credential.cover\"/></template>");
		
		 eResolver.resolve();
		 Element e = eResolver.getRoot();
		
		PrimitiveExpressionResolver primExprResolver = new PrimitiveExpressionResolver();
		primExprResolver.set(e);
		primExprResolver.resolve();
		
		e = (Element) primExprResolver.getTag();
		
		FormData formdata = new FormData();
		formdata.addFieldValue("red", "100");
		formdata.addFieldValue("green", "150");
		formdata.addFieldValue("blue", "255");
		
		SubstitutionExpressionResolver subExprResolver = new SubstitutionExpressionResolver(formdata);
		subExprResolver.set(e);
		subExprResolver.resolve();
		
		
		
	}
	

}
